﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Turners.DMS.WebParts
{
    [ToolboxItemAttribute(false)]
    public class SearchCAWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/Turners.DMS.WebParts/SearchCAWebPart/SearchCAWebPartUserControl.ascx";

        private const string jQueryScriptKey = "jquery";
        private const string jQueryUIScriptKey = "jqueryui";
        private const string jQueryCustomScriptKey = "calendarcustom";
        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        public SearchCAWebPart()
        {
            this.PreRender += new EventHandler(WebPart_ClientScript_PreRender);
        }

        public void WebPart_ClientScript_PreRender(object sender, EventArgs e)
        {
            ClientScriptManager csm = Page.ClientScript;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                string siteUrl = site.Url;
                if (!siteUrl.EndsWith("/"))
                    siteUrl += "/";
                string scriptFolderUrl = siteUrl + "Style Library/js/";
                string styleFolderUrl = siteUrl + "Style Library/styles/";

                CssLink cssLink = new CssLink();
                cssLink.DefaultUrl = styleFolderUrl + "jquery-calendar-ui.css";
                this.Page.Header.Controls.Add(cssLink);

                if (!csm.IsClientScriptIncludeRegistered(jQueryScriptKey))
                    csm.RegisterClientScriptInclude(jQueryScriptKey, scriptFolderUrl + "jquery.js");
                if (!csm.IsClientScriptIncludeRegistered(jQueryUIScriptKey))
                    csm.RegisterClientScriptInclude(jQueryUIScriptKey, scriptFolderUrl + "jquery-ui.js");
                if (!csm.IsClientScriptIncludeRegistered(jQueryCustomScriptKey))
                    csm.RegisterClientScriptInclude(jQueryCustomScriptKey, scriptFolderUrl + "custom.js");

            }

        }
    }
}
